package cn.calm.swing.osgi.mysql.blance.service;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import cn.calm.swing.osgi.api.DefaultProjectService;
import cn.calm.swing.osgi.api.util.EntityUtil;
import cn.calm.swing.osgi.mysql.blance.entity.HostInfo;
import cn.calm.swing.osgi.mysql.blance.ui.MySqlBlanceProjectDialog;

public class MySqlBlanceProjectService extends DefaultProjectService {
	private JPopupMenu contextMenu = new JPopupMenu();
	private static ImageIcon mySqlBlanceProject=new ImageIcon(MySqlBlanceProjectService.class
			.getResource("/project.png"));
	
	public MySqlBlanceProjectService(final BundleContext context) {
		JMenuItem open=new JMenuItem("编辑");
		contextMenu.add(open);
		open.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				HostInfo masterHost = EntityUtil.projectToEntity(getProject(), HostInfo.class, "masterHost");
				List<HostInfo> slaveHosts=EntityUtil.projectToListEntity(getProject(), HostInfo.class, "slaveHosts");
				ServiceReference<MySqlBlanceProjectDialog> serviceReference = context.getServiceReference(MySqlBlanceProjectDialog.class);
				MySqlBlanceProjectDialog service = context.getService(serviceReference);
//				System.out.println(getProject());
//				System.out.println(masterHost);
				service.setProject(getProject());
				service.setMasterHost(masterHost);
				service.setSlaveHosts(slaveHosts);
				service.setVisible(true);
				context.ungetService(serviceReference);
			}
		});
	}

	@Override
	public JPopupMenu getPopupMenu() {
		return contextMenu;
	}

	@Override
	public ImageIcon getIcon() {
		return mySqlBlanceProject;
	}
}
